<?php

namespace app\api\model\operation;
use think\facade\Db;
class UserRecomment{
	
	public function insertData($data){
		return Db::name("user_recomment")->insert($data,true);
	}
	
	public function findRecomment($_where,$_field){
		return Db::name("user_recomment")->alias("r")
			->join("user u","u.id=r.user_id")
			->where($_where)
			->field($_field)
			->find();	
	}
	public function upRecomment($_up,$_where){
		return Db::name("user_recomment")->where($_where)->update($_up);
	}
	public function selectRecomment($_where,$_field,$_limit,$_order){
		$_data=Db::name("user_recomment")->alias("r")
			->join("user u","u.id=r.user_id")
			->where($_where)
			->field($_field)
			->limit(current($_limit),end($_limit))
			->order($_order)
			->select();	
		return empty($_data)?:$_data->toArray();	
	}
	
	public function selectRecommentGroup($_where,$_field,$_limit,$_order,$_group){
		$table="(select * from ".config('database.connections.mysql.prefix')."user_recomment order by id desc) r";
		$_data=Db::name("user")->alias("u")
			->join($table,"r.pid=u.id")
			->where($_where)->field($_field)
			->limit(current($_limit),next($_limit))
			->group($_group)
			->order($_order)
			->select();
		return empty($_data)?:$_data->toArray();	
	}
	
	public function selectRecommentGroupLevel($_where,$_field,$_limit,$_order,$_group,$user_id=0){
		$table="(select * from ".config('database.connections.mysql.prefix')."user_recomment order by id desc) r";
		$table_sql="select ".$_field." from ".config('database.connections.mysql.prefix')."user u 
			join ".$table." on r.pid=u.id 
			where ".$_where." group by ".$_group." order by ".$_order." limit ".current($_limit).",".end($_limit);
		$sql="select * from (select r.*,@rank:=@rank+1 as level from (".$table_sql.") r,(select @rank:=0) t) as t where id=".$user_id;	
		$_rank=Db::Query($sql);
		
		if(empty($_rank[0])){
			return [];
		}
		return $_rank[0];
	}
}
